<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!--Dish list to edit-->
<div class="table-outer">
	<h1 class="center-font">菜品列表</h1>
	<c:choose>
		<c:when test="${foods.size()==0 }">
			<div class="out-container">
				<div class="boo-wrapper">
					<div class="boo">
						<div class="face"></div>
					</div>
					<div class="shadow"></div>
					<h1>Sorry,您还没有上传菜品哟~</h1>
				</div>
			</div>
		</c:when>

		<c:otherwise>
			<table class="table table-hover">
				<thead>
					<tr>
						<th style=" text-align : center;padding-left:8px !important;">菜品图片</th>
						<th style=" text-align : center;padding-left:8px !important;">菜品名称</th>
						<th style=" text-align : center;padding-left:8px !important;">价格/&yen;</th>
						<th style=" text-align : center;padding-left:8px !important;">功能</th>
					</tr>
				</thead>
				<tbody>
					<c:forEach var="f" items="${foods }">
						<tr>
							<td><img src="${myPicServer }${f.fViewPath }" width=30px height=30px></td>
							<td>${f.fName }</td>
							<td>${f.fPrice }</td>
							<td><c:choose>
									<c:when test="${state == 2 }">
										<a href="javascript:void(0)" type="button"
											class="btn btn-danger" id="fIdDel-${f.fId }">下架</a>
									</c:when>
									<c:otherwise>
										<a href="javascript:void(0)" type="button"
											class="btn btn-danger" disabled>下架</a>
									</c:otherwise>
								</c:choose> <c:choose>
									<c:when test="${f.fStatus==1 }">
										<a href="javascript:void(0)" id="fIdRec-${f.fId }"
											hh="fId-${f.fId }&fName-${f.fName}&fViewPath-${f.fViewPath}"
											type="button" class="btn btn-common"><i
											class="glyphicon glyphicon-star" ></i>推荐</a>
									</c:when>

									<c:otherwise>
										<button type="button" 
										id="fIdRec-none"
										 class="btn btn-common" disabled>
											<i class="glyphicon glyphicon-star"></i>已推荐
										</button>
									</c:otherwise>
								</c:choose></td>
						</tr>
					</c:forEach>
				</tbody>
			</table>
		</c:otherwise>
	</c:choose>
</div>
<script type="text/javascript">
	$(function() {
		//删除菜品
		$(".btn.btn-danger").on("click", function() {
			//删除数据库数据
			var btnId = this.id;
			var fId = btnId.split("-")[1];
			$.ajax({
				url : "${pageContext.request.contextPath}/homes/delDish",
				type : "post",
				data : {
					fId : fId
				},
				success : function(d) {
					if (d.status) {
						//删除网页节点
						$("#" + btnId).parent().parent().html("");
					}
				}
			})

		})

		//推荐菜品
		$(".btn.btn-common")
				.on(
						"click",
						function() {
							//修改数据库状态
							var btnId = this.id;
							var hhh=$(this).attr("hh");
							console.log(hhh);
							var fId = hhh.split("&")[0].split("-")[1];
							var fName = hhh.split("&")[1].split("-")[1];
							var fViewPath = hhh.split("&")[2].split("-")[1];
							$
									.ajax({
										url : "${pageContext.request.contextPath}/homes/recommendFood",
										type : "post",
										data : {
											fId : fId,
											fName : fName,
											fViewPath : fViewPath
										},
										success : function(d) {
											if (d.status) {
												//替换网页节点
												console.log($("#" + btnId))
												$("#" + btnId).replaceWith("<button type='button' id='fIdRec-none' class=' btn btn-common' disabled><i class='glyphicon glyphicon-star'></i>已推荐</button>");
											}
										}
									})

						})

	})
</script>
